# https://github.com/bytemain/dotfiles/edit/master/ubuntu_wsl/zshrc
typeset -U PATH
export EDITOR=nvim
export TIME_STYLE='long-iso'
export CC=gcc-9
export GPG_TTY=$(tty)

if type brew &>/dev/null; then
      FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
fi

# alias
alias l="exa -la"
alias ls="exa"
alias la="exa -lah"
alias b=brew
alias vim="nvim"
alias y=yarn
alias py="python3"
alias ipy="ipython"
alias ohmyzsh="cd ~/.oh-my-zsh"
alias vizsh="vim ~/.zshrc"
alias c="code-insiders"
alias e="explorer.exe"
alias q="cd $HOME && clear"
alias cdtmp='cd `mktemp -d /tmp/artin-XXXXXX`'
alias ws="cd ~/0Workspace"
alias le="cd ~/0learn-everything"
alias udtheme="cp -r ~/dotfiles/zsh-theme/. ~/.oh-my-zsh/custom/themes/"
alias udwsl='powershell.exe "sudo powershell -ExecutionPolicy ByPass -File ~\dotfiles\windows\wsl2.ps1"'
alias cls=clear
alias rmrf="rm -rf"
alias srmrf="sudo rm -rf"
alias vimrc="vim ~/.config/nvim/init.vim"
alias ping="nali-ping"
alias dig="nali-dig"
alias d=docker
alias lzd=lazydocker
alias dco="docker-compose"
alias traceroute="nali-traceroute"
alias tracepath="nali-tracepath"
alias nslookup="nali-nslookup"
alias shutdown="wsl.exe --shutdown"
alias clip="clip.exe"
alias apt-update="sudo apt-get update && sudo apt-get -y upgrade"
alias ncdux="ncdu -X ~/dotfiles/_rc/ncduignore"
alias lg=lazygit
alias top=htop
alias fd=fdfind
alias g=git
alias tk=take
alias cg=cargo
alias gcid="git log | head -1 | awk '{print substr(\$2,1,7)}'"
alias src="source ~/.zshrc"

alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

# Enable aliases to be sudo’ed
# http://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo
alias _='sudo '

# Avoid stupidity with trash-cli:
# https://github.com/sindresorhus/trash-cli
# or use default rm -i
if (( $+commands[trash] )); then
  alias rm='trash'
else
  alias rm='rm -i'
fi

if [ -d "$HOME/bin" ] ; then
    export PATH="$HOME/bin:$PATH"
fi

if [ -d "$HOME/.local/bin" ] ; then
    export PATH="$HOME/.local/bin:$PATH"
fi

eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

export PATH="~/.npm-global/bin:$PATH"
export PATH="$HOME/.poetry/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

setopt no_nomatch

# Proxy configuration
getIp() {
    export winip=$(ip route | grep default | awk '{print $3}')
    export wslip=$(hostname -I | awk '{print $1}')
    export PROXY_SOCKS5="socks5://${winip}:7890"
    export PROXY_HTTP="http://${winip}:7890"
}

proxy_git() {
    ssh_proxy="${winip}:7890"
    git config --global http.https://github.com.proxy ${PROXY_HTTP}
    if ! grep -qF "Host github.com" ~/.ssh/config ; then
        echo "Host github.com" >> ~/.ssh/config
        echo "    User git" >> ~/.ssh/config
        echo "    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" >> ~/.ssh/config
    else
        lino=$(($(awk '/Host github.com/{print NR}'  ~/.ssh/config)+2))
        sed -i "${lino}c\    ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" ~/.ssh/config
    fi
}

winip_() {
    getIp
    echo ${winip}
}

wslip_() {
    getIp
    echo ${wslip}
}

x11() {
    getIp
    if [ ! $1 ]; then
        # null
        export DISPLAY=${winip}:0.0
    else
        export DISPLAY=${winip}:$1.0
    fi
    echo $DISPLAY
    export XDG_SESSION_TYPE=x11
    export XDG_RUNTIME_DIR=/tmp/runtime-root
    export LIBGL_ALWAYS_INDIRECT=1
    export PULSE_SERVER=tcp:$winip
}

ip_() {
    getIp
    https --follow -b https://api.ip.sb/geoip/$1
    echo "WIN ip: ${winip}"
    echo "WSL ip: ${wslip}"
}

proxy_npm() {
    getIp
    npm config set proxy ${PROXY_HTTP}
    npm config set https-proxy ${PROXY_HTTP}
    yarn config set proxy ${PROXY_HTTP}
    yarn config set https-proxy ${PROXY_HTTP}
}

unpro_npm() {
    npm config delete proxy
    npm config delete https-proxy
    yarn config delete proxy
    yarn config delete https-proxy
}

proxy() {
    getIp
    # pip can read http_proxy & https_proxy
    export http_proxy="${PROXY_HTTP}"
    export HTTP_PROXY="${PROXY_HTTP}"
    export https_proxy="${PROXY_HTTP}"
    export HTTPS_PROXY="${PROXY_HTTP}"
    export ftp_proxy="${PROXY_HTTP}"
    export FTP_PROXY="${PROXY_HTTP}"
    export rsync_proxy="${PROXY_HTTP}"
    export RSYNC_PROXY="${PROXY_HTTP}"
    export ALL_PROXY="${PROXY_SOCKS5}"
    export all_proxy="${PROXY_SOCKS5}"
    proxy_git
    if [ ! $1 ]; then
        ip_
    fi
    echo "Acquire::http::Proxy \"${PROXY_HTTP}\";" | sudo tee /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
    echo "Acquire::https::Proxy \"${PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1
}

unpro () {
    unset http_proxy
    unset HTTP_PROXY
    unset https_proxy
    unset HTTPS_PROXY
    unset ftp_proxy
    unset FTP_PROXY
    unset rsync_proxy
    unset RSYNC_PROXY
    unset ALL_PROXY
    unset all_proxy
    sudo rm /etc/apt/apt.conf.d/proxy.conf
    git config --global --unset http.https://github.com.proxy
    ip_
}

git-config() {
    echo -n "Please input Git Username: "
    read username
    echo -n "Please input Git Email: "
    read email
    echo -n "Done!"
    git config --global user.name "${username}"
    git config --global user.email "${email}"
    git config --global credential.helper store
    git config --global pull.rebase true
    git config --global alias.s status
    git config --global alias.sb "status -sb"
    git config --global alias.d diff
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.last "log -1 HEAD"
    git config --global alias.cane "commit --amend --no-edit"
    git config --global alias.pr "pull --rebase"
    git config --global alias.lo "log --oneline -n 10"
    git config --global alias.a "add ."
    git config --global alias.cm "commit -m"
    git config --global alias.rh "reset --hard"
    git config --global alias.f "fetch"
}

ssh_start() {
  sshd_status=$(service ssh status)
  if [[ $sshd_status = *"is not running"* ]]; then
    sudo service ssh --full-restart
  fi
}

set_max_user_watches() {
    if ! grep -qF "max_user_watches" /etc/sysctl.conf ; then
        echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
    fi
    sudo sysctl -p >/dev/null 2>&1
    sudo sysctl --system >/dev/null 2>&1
}

expose_local(){
    sudo sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null 2>&1
    sudo iptables -t nat -I PREROUTING -p tcp -j DNAT --to-destination 127.0.0.1
}

put_win_fonts() {
    # 将windows的字体放入ubuntu里
    sudo mkdir /usr/share/fonts/windows
    sudo cp -r /mnt/c/Windows/Fonts/*.ttf /usr/share/fonts/windows/
    fc-cache
}

bk() {
    cp ~/.zshrc ~/dotfiles/ubuntu_wsl/zshrc
    cp ~/.config/nvim/init.vim ~/dotfiles/ubuntu_wsl/init.vim
    cp ~/.config/nvim/coc-settings.json ~/dotfiles/ubuntu_wsl/coc-settings.json
    brew bundle dump --describe --force --no-upgrade --file="~/dotfiles/ubuntu_wsl/Brewfile"
}

ln_never_wrong() {
    ln -s "$1" "$2"
    symlinks -d $(dirname "$2")
    if [ ! -f "$2" ]; then
        echo "bloody hell… I got it wrong again"
        ln -s "$2" "$1"
    fi
}

gpg-login() {
    export GPG_TTY=$TTY
    echo "test" | gpg --clearsign > /dev/null 2>&1
}

gpg-logout() {
    echo RELOADAGENT | gpg-connect-agent
}

u-clean() {
    echo "clean"
    sudo apt-get clean
    echo "auto clean"
    sudo apt-get autoclean -y
    echo "auto remove"
    sudo apt-get autoremove -y
    echo "purge deborphan"
    deborphan | xargs sudo apt-get purge -y
    deborphan
    sudo aptitude search ?obsolete
    echo "aptitude purge"
    sudo aptitude purge ~o
}

u-update() {
    sudo apt-get update && sudo apt-get -y upgrade
    brew upgrade --verbose
    nali update -y
    tldr --update
}

zsh_history_fix() {
    mv ~/.zsh_history ~/.zsh_history_bad
    strings ~/.zsh_history_bad > ~/.zsh_history
    fc -R ~/.zsh_history
    rm ~/.zsh_history_bad
}

cdlast() {
  cd -
  ls -lrth --color=auto | tail
  zle reset-prompt
}
zle -N cdlast
bindkey '^Q' cdlast

# Kills a process running on a specified tcp port
killport() {
  for port in "$@"
  do
    echo "Killing process on port: $port"
    fuser -n tcp -k $port;
  done
}

# MVP
# Move and make parent directories
mvp() {
    source="$1"
    target="$2"
    target_dir="$(dirname "$target")"
    mkdir --parents $target_dir; mv $source $target
}

cleantmp() {
    sudo find /tmp -type f -atime +10 -delete
}

eval "$(pyenv init -)"
eval "$(starship init zsh)"

if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then
    print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma/zinit%F{220})…%f"
    command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit"
    command git clone https://github.com/zdharma/zinit "$HOME/.zinit/bin" && \
        print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \
        print -P "%F{160}▓▒░ The clone has failed.%f%b"
fi

source "$HOME/.zinit/bin/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

zinit snippet OMZP::sudo
zinit snippet OMZ::lib/history.zsh
zinit snippet OMZP::extract

zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-syntax-highlighting
zinit light sukkaw/zsh-ipip
zinit light agkozak/zsh-z
